var _modules = ["common", "eventManager", "queryString", "coAjax", "hash_routie", "routie", "serverNotify", "href", './js/tools/getarea.js'];
if (_isWX) {
	_modules.push("WX");
};
require(_modules, function(jSouper, eventManager, QueryString, coAjax, hash_routie, routie, serverNotify, href, getarea, WX) {
	/*
	 * Sock连接
	 */
	var user_notify = serverNotify("user");
	function _set_pay_ach(pay_ach) {
		pay_ach._auto_go_online_pay = true;
		App.set("user.pay_ach", pay_ach);
	};

	//订单支付成功后的页面回调
	function _pay_success_cb(new_ach_info) {
		_set_pay_ach(new_ach_info);
		href.jump("/mobile.personal.html");
	};
	user_notify.on("pay_success", _pay_success_cb);

	user_notify.on("ach.state.changed", function(ach_info) {
		if (App.get("user.pay_ach._id") == ach_info.ach_id) {
			App.set("user.pay_ach.info.state", ach_info.state);
		};
	});

	App.set("_is_weixin", _isWX);

	hash_routie.on("pay", function() {
		var queryString = new QueryString(location.hash);
		var ach_id = queryString.get("ach_id");
		console.log(ach_id);
		if (!ach_id) {
			alert("error", "无效的订单编号");
		};
		// 获取订单信息
		coAjax.get(appConfig.user.ach_by_id + ach_id, function(result) {
			var _user_ach = result.result;
			_set_pay_ach(_user_ach);
			// 订单取消
			App.set("user.pay_ach.$Event.cancel_ach_success", function(vm, result) {
				href.toMain();
			});
			// 获取用户优惠券列表
			coAjax.get(appConfig.user.ticket_list, function(ticket_result) {
				var bus_tickets = ticket_result.result.filter(function(a) {
					return a.bus_id = App.get("bus_info.bus_id");
				});
				var no_use_tickets = bus_tickets.filter(function(b) {
					return b.state == 1;
				});
				var can_use_tickets = no_use_tickets.filter(function(c) {
					return c.ticket_factory.info.config.full_cash < _user_ach.sou_finally_cash;
				});
				App.set("$Cache.pay.tickets", can_use_tickets);
			});
		}, function(errorCode) {
			console.log("errorCode:", errorCode);
			alert("error", "无效的订单编号");
			myConfirm("订单无效，是否回到店铺首页", function() {
				href.toMain();
			});
		});

		//获取用户地址列表
		coAjax.get(appConfig.user.address_detail, function(result) {
			App.set("user.address_detail", result.result);
			App.getModel().touchOff("user.pay_ach.info.user_address")
		});

	});
	App.set("$Event.cancel_ach_success", function(vm, result) {
		href.toMain();
	});
	//改变支付方式
	App.set("$Event.pays.change_pay_method", function(e) {
		var pay_method = this.getAttribute("value");
		if (!(App.get("user.pay_ach.info.pay_method") == pay_method)) {
			coAjax.put(appConfig.user.ach_change_pay_method + App.get("user.pay_ach._id"), {
				pay_method: pay_method
			}, function(result) {
				_set_pay_ach(result.result);
				alert("success", "支付方式修改成功");
			}, function(errorCode) {
				alert("error", "支付方式修改失败");
			})
		};
	});
	// 添加地址
	App.set("$Event.add_user_address", function() {
		var new_user_address = {
			province: $("[name='province']").val(),
			town: $("[name='city']").val(),
			county: $("[name='area']").val(),
			// street:
			detail: $("[name='detailed_addre']").val(),
			mobilenumber: $("[name='mobile_num']").val(),
			name: $("[name='name']").val()
		};
		console.log(new_user_address);
		coAjax.post(appConfig.user.add_address, new_user_address, function(result) {
			var address_detail = App.get("user.address_detail");
			address_detail.unshift(result.result);
			App.set("user.address_detail", address_detail);
			App.getModel().touchOff("user.pay_ach.info.user_address");
			App.model.toggle('isadd');
			coAjax.put(appConfig.user.ach_change_user_address + App.get("user.pay_ach._id"), {
				user_address_id: result.result._id
			}, function(result) {
				alert('success', "收货地址修改成功");
				App.set("user.pay_ach.info.user_address", result.result);
			}, function(errorCode, xhr, errorMsg) {
				App.getModel().touchOff("user.pay_ach.info.user_address._id")
				alert("error", errorMsg);
			});
			//设置为默认地址
			coAjax.put(appConfig.user.set_default_address, {
				address_id: result.result._id
			}, function(result) {
				App.set("user.default_ua_id", result.result);
				console.log("默认地址修改成功");
			});
		}, function(errorCode, xhr, errorMsg) {
			alert("error", errorMsg);
		});
	});
	//改变订单使用的收获地址
	App.set("$Event.pays.change_user_address", function(e, vm) {
		coAjax.put(appConfig.user.ach_change_user_address + App.get("user.pay_ach._id"), {
			user_address_id: vm.get("_id")
		}, function(result) {
			App.model.toggle('address');
			App.set("user.pay_ach.info.user_address", result.result);
		});
		//设置为默认地址
		coAjax.put(appConfig.user.set_default_address, {
			address_id: vm.get("_id")
		}, function(result) {
			App.set("user.default_ua_id", result.result);
			console.log("默认地址修改成功");
		});
	});
	//使用支付宝下单
	App.set("$Event.pays.alipay_build", function() {
		require("coAjax").post(appConfig.server_url + "alipay/create_direct_pay_by_user", {
			WIDout_trade_no: App.get("user.pay_ach._id"),
			WIDsubject: "来自“" + App.get("bus_info.bus_id") + "”的订单",
			WIDtotal_fee: App.get("user.pay_ach.cash"),
			WIDbody: "",
			WIDshow_url: window.location.toString(),
		}, function(result) {
			// result.result.appendTo(document.body);
			App.set("$Cache.alipay_submit_pay_ach", result.toString);
		}, function(ec, xhr, em) {
			console.log(em)
		});
	});

	// 选择优惠券
	App.set("$Event.pay.choose_ticket", function(e, vm) {
		if (App.get("$Cache.pay.ticket_id") == vm.get("_id")) {
			App.set("$Cache.pay.ticket_id", "");
			return;
		};
		App.set("$Cache.pay.ticket_id", vm.get("_id"));
	});

	// 使用并绑定优惠券
	App.set("$Event.pay.using_checked_ticket", function() {
		if (!App.get("$Cache.pay.ticket_id")) {
			myConfirm("请先点击要使用的优惠券，再次点击优惠券可取消使用。");
			return;
		};
		myConfirm("确定使用该优惠券吗？", function() {
			coAjax.put(appConfig.user.ach_bind_ticket, {
				ach_id: App.get("user.pay_ach._id"),
				ticket_id: App.get("$Cache.pay.ticket_id")
			}, function(result) {
				alert("优惠码绑定成功");
				console.log(result.result);
				_set_pay_ach(result.result);
			});
		});
	});
});